import {AfterViewInit, Component, OnInit, ViewChild} from '@angular/core';
import {MessageService} from '../../../../../ng-tools';
import {TicketService} from '../../../../services/api/ticket.service';
import {HttpParams} from '@angular/common/http';

@Component({
  selector: 'platform-statistic',
  templateUrl: './platform-statistic.component.html',
  styleUrls: ['./platform-statistic.component.scss']
})
export class PlatformStatisticComponent implements OnInit, AfterViewInit {
  breadcrumb = [
    {name: '平台统计', cssIcon: 'fa fa-fw fa-th-list'}
  ];
  @ViewChild('start_date_time') start_date_time;
  @ViewChild('end_date_time') end_date_time;
  platformInfo;
  defaultStartTime;
  defaultEndTime;
  constructor(private message: MessageService,
              private ticketService: TicketService) { }

  ngOnInit() {
    const date = new Date().toLocaleDateString();
    const currentDate = date.replace(/\//g, '-');
    this.defaultStartTime = currentDate + ' 00:00:00';
    this.defaultEndTime = currentDate + ' 23:59:59';
    this.getStatisticInfo(this.defaultStartTime, this.defaultEndTime);
  }
  search() {
    this.getStatisticInfo(this.start_date_time.nativeElement.value.replace(/\//g, '-') + ':00', this.end_date_time.nativeElement.value.replace(/\//g, '-') + ':00');
  }
  getStatisticInfo(startTime, endTime) {
    this.ticketService.get(this.ticketService.urls.getPlatAccountInfo, {
      observe: 'response',
      params: new HttpParams()
          .append('web_token', localStorage.getItem('token'))
          .append('d1', startTime)
          .append('d2', endTime)}).subscribe((resp) => {
      if (resp.body.success) {
              this.platformInfo = resp.body.returnValue;
        this.message.sendMessage('page load progress', this);
            }
    });
  }
  ngAfterViewInit(): void {
    this.message.sendMessage('breadcrumb changed', this, this.breadcrumb);
    this.message.sendMessage('page load progress', this, '300ms');
  }

}
